import { _decorator, Component, tween, Vec3 } from 'cc';
import { Game } from './game';
const { ccclass } = _decorator;

@ccclass('Block')
export class Block extends Component {

    pos_dianWorld: Vec3;
    pos_dianParent: Vec3;
    arrChildren: any;
    canTouch: boolean;
    score: number;
    isPZ: boolean;
    blockType: any;

    onLoad() {
        this.canTouch = true;
        this.arrChildren = this.node.children;
    }

    init(pos_dian: any, blockType: any, isFirst: any) {
        this.score = 0;
        this.isPZ = false;
        this.canTouch = false;
        this.blockType = blockType;
        this.pos_dianParent = this.arrChildren[blockType].getChildByName('dian').getPosition();
        var pos_node = new Vec3(pos_dian.x - this.pos_dianParent.x, pos_dian.y - this.pos_dianParent.y);
        this.node.setPosition(new Vec3(pos_node.x, pos_node.y + 100));
        // var act_1 = cc.moveTo(0.2, cc.v2(pos_node.x, pos_node.y - 10));
        // var act_2 = cc.moveTo(0.05, cc.v2(pos_node.x, pos_node.y + 10));
        // var act_3 = cc.moveTo(0.05, cc.v2(pos_node.x, pos_node.y - 3));
        // var act_4 = cc.moveTo(0.03, cc.v2(pos_node.x, pos_node.y));
        // var act_5 = cc.callFunc(function () {
        //     game.moveDir();
        // }, this)
        // var act_end = cc.sequence(act_1, act_2, act_3, act_4, act_5);
        const act_1 = tween(this.node).to(0.2, { position: new Vec3(pos_node.x, pos_node.y - 10, 0) });
        const act_2 = tween(this.node).to(0.05, { position: new Vec3(pos_node.x, pos_node.y + 10, 0) });
        const act_3 = tween(this.node).to(0.05, { position: new Vec3(pos_node.x, pos_node.y - 3, 0) });
        const act_4 = tween(this.node).to(0.03, { position: new Vec3(pos_node.x, pos_node.y, 0) });
        const act_5 = tween(this.node).call(() => {
            Game.instance.moveDir(); // 根据 Game 类的定义调用方法
        });

        // 动作顺序
        const act_end = tween(this.node).sequence(act_1, act_2, act_3, act_4, act_5);
        if (isFirst < 3) {
            this.canTouch = true;
            this.node.setPosition(pos_node);
            if (isFirst == 1) {
                this.score = 1;
            }
        } else {
            //this.node.runAction(act_end);
            act_end.start();
        }
        this.pos_dianWorld = pos_dian;
        this.setBlock();
    }

    playAnima() {
        var anim = this.arrChildren[this.blockType].getChildByName('block').getComponent(Animation);
        if (anim) {
            anim.play();
        }
    }

    setBlock() {
        var children = this.node.children;
        for (let i = 0; i < children.length; i++) {
            if (i == this.blockType) {
                children[i].active = true;
            } else {
                children[i].active = false;
            }
        }
    }

    setDianPos() {
        this.canTouch = true;
        var pos_node = this.node.getPosition();
        this.pos_dianWorld = new Vec3(pos_node.x + this.pos_dianParent.x, pos_node.y + this.pos_dianParent.y);
    }

    touchBegin(dt: any) {
        this.node.scale = new Vec3(this.node.scale.x, this.node.scale.y - dt / 6, this.node.scale.z);
        //this.node.scaleY = this.node.scale.y - dt / 6;
        if (this.node.scale.y <= 0.8) {
            this.node.scale = new Vec3(this.node.scale.x, 0.8, this.node.scale.z);
        }

        Game.instance.hero.scale = new Vec3(Game.instance.hero.scale.x + dt / 10,this.node.scale.y,Game.instance.hero.scale.z);
        // Game.instance.hero.scaleY = this.node.scaleY;
        // Game.instance.hero.scaleX = Game.instance.hero.scaleX + dt / 10;
        if (Game.instance.hero.scaleX >= 1.2) {
            Game.instance.hero.scaleX = 1.2;
        }
        if (this.node.scale.y > 0.8) {
            Game.instance.hero.y = Game.instance.hero.y - dt / 8 * this.arrChildren[this.blockType].getChildByName('block').height;
        };
    }

    touchEnd() {
        Game.instance.hero.scale = new Vec3(1,1,1);
        // Game.instance.hero.scaleX = 1;
        // Game.instance.hero.scaleY = 1;
        var scale_Y = 1 - this.node.scale.y;
        // var act_1 = cc.scaleTo(scale_Y / 5, 1 + scale_Y / 2);
        // var act_2 = cc.scaleTo(scale_Y / 8, 1 - scale_Y / 5);
        // var act_3 = cc.scaleTo(scale_Y / 10, 1);
        // var end = cc.sequence(act_1, act_2, act_3);
        // this.node.runAction(end);
        var act_1 = { scale: new Vec3(1 + scale_Y / 2, 1 + scale_Y / 2, 1) };
        var act_2 = { scale: new Vec3(1 - scale_Y / 5, 1 - scale_Y / 5, 1) };
        var act_3 = { scale: new Vec3(1, 1, 1) };
        tween(this.node)
        .to(scale_Y / 5, act_1)
        .to(scale_Y / 8, act_2)
        .to(scale_Y / 10, act_3)
        .start();
    }

    update(dt: any) {
    }

}